<?php

/**
 * 供应商联系人
 */

namespace app\admin\service;

use think\Service;
use app\admin\model\SupplierContact as SupplierContactModel;

class SupplierContact extends Service
{

    /**
     * 获取供应商联系人列表
     * @param $param
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($param = [], $page = 1, $limit = 10)
    {
        $arrData = ['code' => 0, 'message' => 'success'];
        $where[] = ['is_del', '=', 0];
        if ($param['supid']) {
            $where[] = ['supid', '=', $param['supid']];
        }
        if ($param['cname']) {
            $where[] = ['cname', 'like', '%' . $param['cname'] . '%'];
        }
        if ($param['phone']) {
            $where[] = ['phone', 'like', '%' . $param['phone'] . '%'];
        }
        if ($param['email']) {
            $where[] = ['email', 'like', '%' . $param['email'] . '%'];
        }
        if ($param['wechat']) {
            $where[] = ['wechat', 'like', '%' . $param['wechat'] . '%'];
        }

        $oSupplierContactModel = SupplierContactModel::where($where);
        $total = $oSupplierContactModel->count();
        $result = $oSupplierContactModel
            ->with(['get_creator_name', 'get_update_name', 'get_supplier_name', 'get_country_name'])
            ->append(['gender_value'])
            ->order('id', 'desc')
            ->page($page, $limit)
            ->select();
        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }

    /**
     * 删除单条或多条供应商联系人数据
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($param)
    {
        $where = [['is_del', '=', '0'], ['id', 'in', $param['ids']]];
        $oSupplierContactModel = SupplierContactModel::where($where);
        $brand = $oSupplierContactModel->find();
        if (!$brand) {
            return ['code' => 1, 'message' => '获取供应商联系人信息失败！'];
        }
        $result = $oSupplierContactModel->update(['is_del' => time()]);
        if ($result) {
            return ['code' => 0, 'message' => '删除供应商联系人成功！'];
        } else {
            return ['code' => 1, 'message' => '删除供应商联系人失败！'];
        }
    }

    /**
     * 添加修改供应商联系人
     * @param $param
     * @return array|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save($param)
    {
        $checkSupplierContactModel = SupplierContactModel::where([['cname', '=', $param['cname']], ['is_del', '=', 0]]);
        if ($param['id']) {
            $checkSupplierContactModel->where('id', '<>', $param['id']);
        }
        if ($checkSupplierContactModel->find()) {
            return ['code' => 1, 'message' => '供应商联系人名称重复！'];
        }

        if ($param['id']) {
            $oSupplierContactModel = SupplierContactModel::find($param['id']);
            $oSupplierContactModel->updater = $param['operator_id'];
            $oSupplierContactModel->update_time = date('Y-m-d H:i:s', time());
        } else {
            $oSupplierContactModel = new SupplierContactModel();
            $oSupplierContactModel->creator = $param['operator_id'];
            $oSupplierContactModel->create_time = date('Y-m-d H:i:s', time());
            $oSupplierContactModel->is_del = 0;
        }

        $oSupplierContactModel->cname = $param['cname'];
        $oSupplierContactModel->supid = $param['supid'];
        $oSupplierContactModel->gender = $param['gender'];
        $oSupplierContactModel->birthday = $param['birthday'];
        $oSupplierContactModel->phone = $param['phone'];
        $oSupplierContactModel->email = $param['email'];
        $oSupplierContactModel->qq = $param['qq'];
        $oSupplierContactModel->wechat = $param['wechat'];
        $oSupplierContactModel->whatsapp = $param['whatsapp'];
        $oSupplierContactModel->dingtalk = $param['dingtalk'];
        $oSupplierContactModel->country = $param['country'];
        $oSupplierContactModel->address = $param['address'];
        $oSupplierContactModel->department = $param['department'];
        $oSupplierContactModel->position = $param['position'];
        $oSupplierContactModel->level = $param['level'];
        $oSupplierContactModel->tags = $param['tags'];
        $oSupplierContactModel->remark = $param['remark'];

        if ($oSupplierContactModel->save()) {
            return ['code' => 0, 'message' => '保存供应商联系人成功！'];
        }
    }

    /**
     * 获取单个供应商联系人信息
     * @param int $id 供应商联系人ID
     * @return array
     */
    public function getInfo($param)
    {
        $where = [['is_del', '=', '0'], ['id', '=', $param['id']]];
        $oSupplierContactModel = SupplierContactModel::where($where)->with(['get_creator_name', 'get_update_name', 'get_supplier_name', 'get_country_name'])
            ->append(['gender_value'])->find();
        if (!$oSupplierContactModel) {
            return ['code' => 1, 'message' => '获取供应商联系人信息失败！'];
        }
        return ['code' => 0, 'message' => 'success', 'data' => $oSupplierContactModel];
    }

    public function getFieldEnum($field)
    {
        $model = new SupplierContactModel();
        $res = $model->getFieldEnum($field);
        return [
            'code' => 0,
            'data' => $res
        ];
    }

    /**
     * 获取下拉框选项
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSelect()
    {
        $where = [['is_del','=',0]];
        $arrData = ['code' => 0, 'message' => 'success'];

        $oCategoryModel = SupplierContactModel::where($where);
        $total = $oCategoryModel->count();
        $result = $oCategoryModel->field('id AS value,supname as label')->select();
        $arrData['data'] = ['list' => $result->toArray(), 'total' => $total];
        return $arrData;
    }
}
